package twentytwentyone_3;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2021-03-25 9:33
 * @Modified By:
 */
public class PivotIndex_724 {
    public static void main(String[] args){
        int[] nums = new int[]{2, 1, -1};
        System.out.println(pivotIndex(nums));
    }

    public static int pivotIndex(int[] nums){
        // 用i来标记中心下标的位置
        for (int i = 0; i < nums.length; i++) {
            int left = i,right = i;
            int sumLeft = 0;
            int sumRight = 0;
            while (left > 0){
                left --;
                sumLeft += nums[left];
            }
            while (right < nums.length - 1){
                right ++;
                sumRight += nums[right];
            }
            if (sumLeft == sumRight){
                return i;
            }
        }
        return -1;
    }
}
